//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    username: '',
    password: '',
    radioId: "",
    type: 0,
    items: [{
        name: '学生',
        checked: true
      },
      {
        name: '老师',
        checked: false
      }
    ],
    input: '学号'
  },
  changeSex: function (e) {
    var value = e.detail.value;
    //  console.log(value)
    if (value == '学生') {
      this.setData({
        type: 0,
        input: '学号'
      })
    } else {
      this.setData({
        type: 1,
        input: '职工号'
      })
    }
  },

  onShow: function () {
    // 生命周期函数--监听页面显示
    wx.hideTabBar({})
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '登录'
    })
  },


  // 获取输入账号 
  usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  reg: function () {

    wx.navigateTo({

      url: '../reg/reg',
    })

  },
  regter: function () {
    wx.navigateTo({

      url: '../terreg/terreg',
    })
  },
  // 登录处理
  login: function () {
    var that = this;
    if (this.data.username.length == 0 || this.data.password.length == 0) {
      wx.showToast({
        title: '名字或密码不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.baseUrl + '/user/login', // 仅为示例，并非真实的接口地址
        method: 'post',
        data: {
          account: that.data.username,
          pwd: that.data.password,
          type: that.data.type,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(res) {
          if (res.data.code == 0 && res.data.data != null) {


            var id = res.data.data.id;
            var password = res.data.data.pwd;
            var xueyuan = res.data.data.xueyuan;
            var uimg = res.data.data.img;
            var uname = res.data.data.name;
            wx.setStorageSync('uid', id);
            wx.setStorageSync('pwd', password);
            wx.setStorageSync('uimg', uimg);
            wx.setStorageSync('uname', uname);
            wx.setStorageSync('xueyuan', xueyuan);
            if (that.data.type == 0) {
              wx.switchTab({
                url: '../main/main'
              })
            } else {
              wx.navigateTo({
                url: '../course/course',
              })
            }

          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  }
})